<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Gosub</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<link href="../static/theme.css" rel="stylesheet" type="text/css" />
<script src="../static/content.js" type="text/javascript"></script>
</head>
<body>

<h1>Gosub</h1>

<p>Jumps to the specified label and continues execution until <a href="Return.htm">Return</a> is encountered.</p>

<pre class="Syntax">Gosub, Label</pre>
<h3>Parameters</h3>
<dl>

  <dt>Label</dt>
  <dd><p>The name of the <a href="../misc/Labels.htm">label</a>, <a href="../Hotkeys.htm">hotkey label</a>, or <a href="../Hotstrings.htm#label">hotstring label</a> to which to jump, which causes the commands beneath <em>Label</em> to be executed until a Return or Exit is encountered. <a href="Return.htm">&quot;Return&quot;</a> causes the script to jump back to the first command beneath the Gosub and resume execution there. <a href="Exit.htm">&quot;Exit&quot;</a> terminates the <a href="../misc/Threads.htm">current thread</a>.</p></dd>

</dl>

<h3>Remarks</h3>
<p>As with the parameters of almost all other commands, <em>Label</em> can be a <a href="../Variables.htm">variable</a> reference such as %MyLabel%, in which case the name stored in the variable is used as the target. However, performance is slightly reduced because the target label must be &quot;looked up&quot; each time rather than only once when the script is first loaded.</p>
<p>When using a dynamic label such as %MyLabel%, an error dialog will be displayed if the label does not exist. To avoid this, call <a href="../Functions.htm#IsLabel">IsLabel()</a> beforehand. For example:</p>
<pre>if IsLabel(VarContainingLabelName)
    Gosub %VarContainingLabelName%</pre>
<p>Although Gosub is useful for simple, general purpose subroutines, consider using <a href="../Functions.htm">functions</a> for more complex purposes.</p>
<h3>Related</h3>
<p><a href="Return.htm">Return</a>, <a href="../Functions.htm">Functions</a>, <a href="../Functions.htm#IsLabel">IsLabel()</a>, <a href="Block.htm">Blocks</a>, <a href="Loop.htm">Loop</a>, <a href="Goto.htm">Goto</a></p>
<h3>Example</h3>
<pre class="NoIndent">Gosub, Label1 
MsgBox, The Label1 subroutine has returned (it is finished).
return

Label1:
MsgBox, The Label1 subroutine is now running.
return</pre>

</body>
</html>
